package com.sleepless.my.service.impl;

import com.sleepless.my.entity.UsersEntity;
import com.sleepless.my.entity.VideoEntity;
import com.sleepless.my.io.RestBean;
import com.sleepless.my.io.request.Video.CreateVideoReq;
import com.sleepless.my.io.request.Video.GetVideosReq;
import com.sleepless.my.io.response.PageInfo;
import com.sleepless.my.io.response.Video.GetVideoResp;
import com.sleepless.my.io.response.Video.GetVideosResp;
import com.sleepless.my.repository.UsersRepository;
import com.sleepless.my.repository.VideoRepository;
import com.sleepless.my.service.VideoService;
import com.sleepless.my.utils.FileUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import jakarta.persistence.criteria.Predicate;
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {
    @Resource
    private FileUtil fileUtil;

    @Resource
    private UsersRepository usersRepository;

    @Resource
    private VideoRepository videoRepository;

    @Override
    public Object createVideo(MultipartFile file, CreateVideoReq req) {
        UsersEntity user = usersRepository.findByuuid(req.getUuid());
        if(user == null) {
            return RestBean.Code.用户不存在;
        }

        String fileName = fileUtil.saveFile(file,FileUtil.FileType.VIDEO);
        String coverName=fileUtil.createThumbnail(fileName);
        VideoEntity videoEntity = VideoEntity.builder()
                .title(req.getTitle())
                .description(req.getDescription())
                .srcURL(fileName) // 假设文件保存后返回文件名或路径
                .cover(coverName)
                .usersEntity(user)
                .videoType(req.getVideoType())
                .build();
        videoRepository.save(videoEntity);
        return videoEntity.getVideoID();
    }

    @Override
    public Object getRandomVideo() {
        List<Integer> videoIds = videoRepository.findRandomIds(10);
        if (videoIds == null) {
            return RestBean.Code.服务错误;
        }
        if(videoIds.isEmpty()){
            return RestBean.Code.视频不存在;
        }
        Random random = new Random();
        int randomIndex = random.nextInt(videoIds.size());

        return videoIds.get(randomIndex);
    }

    @Override
    public Object getVideos(GetVideosReq req) {
        Pageable pageable = PageRequest.of(req.getPageReq().getPage(), req.getPageReq().getLimit());
        Specification<VideoEntity> spec = (root, query, cb) ->{
            List<Predicate>predicates=new ArrayList<>();
            return cb.and(predicates.toArray(new Predicate[0]));
        };
        Page<VideoEntity> page=videoRepository.findAll(spec, pageable);
        List<VideoEntity> videos = page.getContent();
        GetVideoResp[] getVideoResps = new GetVideoResp[videos.size()];
        for (VideoEntity video : videos) {
            UsersEntity usersEntity=video.getUsersEntity();
            GetVideoResp getVideoResp = GetVideoResp.builder()
                    .videoID(video.getVideoID())
                    .title(video.getTitle())
                    .description(video.getDescription())
                    .srcURL(video.getSrcURL())
                    .cover(video.getCover())
                    .videoType(video.getVideoType())
                    .authorUsername(usersEntity.getUsername())
                    .authorUuid(usersEntity.getUuid())
                    .authorAvatar(usersEntity.getAvatar())
                    .createTime(video.getCeateTime())
                    .build();
            getVideoResps[videos.indexOf(video)] = getVideoResp;
        }
        return GetVideosResp.builder()
                .videos(getVideoResps)
                .pageInfo(new PageInfo<>(page))
                .build();
    }

    @Override
    public Object getVideo(int videoId){
        VideoEntity videoEntity=videoRepository.findById(videoId).orElse(null);
        UsersEntity usersEntity=videoEntity.getUsersEntity();
        return GetVideoResp.builder()
                .videoID(videoEntity.getVideoID())
                .videoType(videoEntity.getVideoType())
                .srcURL(videoEntity.getSrcURL())
                .cover(videoEntity.getCover())
                .title(videoEntity.getTitle())
                .description(videoEntity.getDescription())
                .authorUsername(usersEntity.getUsername())
                .authorUuid(usersEntity.getUuid())
                .authorAvatar(usersEntity.getAvatar())
                .createTime(videoEntity.getCeateTime())
                .build();
    }
}
